package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;

import com.pojo.User;
import com.service.UserService;

@Controller
public class UserController {
	@Autowired
    private UserService userService;
    @GetMapping("/login")
    public String showLogin(){
        return "login";
    }
    @PostMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, ModelMap m){
        User user=userService.login(username, password);
        if(user!=null){
            return "redirect:index.jsp";
        }else{
            m.put("msg","登录失败，账号或密码错误！");
            return "login";
        }
    }
    @GetMapping("/register")
    public String showRegister(){
        return "register";
    }
    @PostMapping("/register")
    public String register(@ModelAttribute("user")  User user,@RequestParam("cardNumber") String cardNumber,@RequestParam("taocan") Integer taocan, Model model,HttpServletRequest request){
    	
    	user.setCardNumber(cardNumber);
    	user.setTaocan(taocan);
    	System.out.println("============="+user);
        int result=userService.register(user);
        if(result==0){
            model.addAttribute("msg","注册失败，请重新输入！");
            return "register";
        }
        return "redirect:/login"; 
    }

}
